package com.lyf.scm.plan.domain.util;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.lyf.scm.common.constants.CommonConstants;
import com.lyf.scm.common.enums.OperationDataEnum;

import java.util.Date;
import java.util.Set;

/**
 * 大数据缓存工具类
 *
 * @author wangchlei
 * @date 2020/09/30
 */
public class BigDataCacheUtil {
    /**
     * 当前日期（字符串）
     */
    public static String nowDate;
    /**
     * 日维度信息————起始日期
     */
    public static Date dayStartDate;
    public static String dayStartDateStr;
    /**
     * 日维度信息————截止更新日期
     */
    public static Date dayUpEndDate;
    public static String dayUpEndDateStr;
    public static Date day1;
    public static String dayStr1;
    public static Date day2;
    public static String dayStr2;
    public static Date day3;
    public static String dayStr3;
    public static Date day4;
    public static String dayStr4;
    public static Date day5;
    public static String dayStr5;
    public static Date day6;
    public static String dayStr6;
    public static Date day7;
    public static String dayStr7;
    public static Date day8;
    public static String dayStr8;
    public static Date day9;
    public static String dayStr9;
    public static Date day10;
    public static String dayStr10;
    public static Date day11;
    public static String dayStr11;
    public static Date day12;
    public static String dayStr12;
    public static Date day13;
    public static String dayStr13;
    public static Date day14;
    public static String dayStr14;
    /**
     * 周维度信息————起始日期
     */
    public static Date weekStartDate;
    public static String weekStartDateStr;
    /**
     * 周维度信息————截止更新日期
     */
    public static Date weekUpEndDate;
    public static Date startDate1;
    public static String startDateStr1;
    public static Date startDate2;
    public static String startDateStr2;
    public static Date startDate3;
    public static String startDateStr3;
    public static Date startDate4;
    public static String startDateStr4;
    public static Date startDate5;
    public static String startDateStr5;
    public static Date startDate6;
    public static String startDateStr6;
    public static Date startDate7;
    public static String startDateStr7;
    public static Date startDate8;
    public static String startDateStr8;
    public static Date startDate9;
    public static String startDateStr9;
    public static Date startDate10;
    public static String startDateStr10;
    public static Date startDate11;
    public static String startDateStr11;
    public static Date startDate12;
    public static String startDateStr12;
    public static Date startDate13;
    public static String startDateStr13;
    public static Date endDate1;
    public static String endDateStr1;
    public static Date endDate2;
    public static String endDateStr2;
    public static Date endDate3;
    public static String endDateStr3;
    public static Date endDate4;
    public static String endDateStr4;
    public static Date endDate5;
    public static String endDateStr5;
    public static Date endDate6;
    public static String endDateStr6;
    public static Date endDate7;
    public static String endDateStr7;
    public static Date endDate8;
    public static String endDateStr8;
    public static Date endDate9;
    public static String endDateStr9;
    public static Date endDate10;
    public static String endDateStr10;
    public static Date endDate11;
    public static String endDateStr11;
    public static Date endDate12;
    public static String endDateStr12;
    public static Date endDate13;
    public static String endDateStr13;
    public static boolean shopFlag;
    public static boolean directlyFlag;
    public static boolean executeFlag;
    public static boolean accuracyFlag;
    public static boolean differinFlag;
    public static boolean demandFlag;

    public static Date startTime;
    public static Date endTime;


    public static void injectDayCache(Date date) {
        nowDate = DateUtil.format(date, DatePattern.NORM_DATE_PATTERN);
        day1 = DateUtil.beginOfDay(date);
        dayStartDate = day1;
        dayStartDateStr = DateUtil.format(dayStartDate, DatePattern.NORM_DATE_PATTERN);
        dayStr1 = dayStartDateStr;
        day2 = getNextDay(day1);
        dayStr2 = DateUtil.format(day2, DatePattern.NORM_DATE_PATTERN);
        day3 = getNextDay(day2);
        dayStr3 = DateUtil.format(day3, DatePattern.NORM_DATE_PATTERN);
        day4 = getNextDay(day3);
        dayStr4 = DateUtil.format(day4, DatePattern.NORM_DATE_PATTERN);
        day5 = getNextDay(day4);
        dayStr5 = DateUtil.format(day5, DatePattern.NORM_DATE_PATTERN);
        day6 = getNextDay(day5);
        dayStr6 = DateUtil.format(day6, DatePattern.NORM_DATE_PATTERN);
        day7 = getNextDay(day6);
        dayStr7 = DateUtil.format(day7, DatePattern.NORM_DATE_PATTERN);
        day8 = getNextDay(day7);
        dayStr8 = DateUtil.format(day8, DatePattern.NORM_DATE_PATTERN);
        day9 = getNextDay(day8);
        dayStr9 = DateUtil.format(day9, DatePattern.NORM_DATE_PATTERN);
        day10 = getNextDay(day9);
        dayStr10 = DateUtil.format(day10, DatePattern.NORM_DATE_PATTERN);
        day11 = getNextDay(day10);
        dayStr11 = DateUtil.format(day11, DatePattern.NORM_DATE_PATTERN);
        day12 = getNextDay(day11);
        dayStr12 = DateUtil.format(day12, DatePattern.NORM_DATE_PATTERN);
        day13 = getNextDay(day12);
        dayUpEndDate = day13;
        dayUpEndDateStr = DateUtil.format(dayUpEndDate, DatePattern.NORM_DATE_PATTERN);
        dayStr13 = dayUpEndDateStr;
        day14 = getNextDay(day13);
        dayStr14 = DateUtil.format(day14, DatePattern.NORM_DATE_PATTERN);
    }

    public static void injectWeekCache(Date date) {
        weekStartDate = DateUtil.beginOfWeek(date);
        weekUpEndDate = DateUtil.beginOfDay(DateUtil.endOfWeek(DateUtil.offset(weekStartDate, DateField.DAY_OF_WEEK, 11)));
        startDate1 = weekStartDate;
        weekStartDateStr = DateUtil.format(weekStartDate, DatePattern.NORM_DATE_PATTERN);
        startDateStr1 = weekStartDateStr;
        endDate1 = getNextWeekEndDay(startDate1);
        endDateStr1 = DateUtil.format(endDate1, DatePattern.NORM_DATE_PATTERN);
        startDate2 = getNextWeekStartDay(startDate1);
        startDateStr2 = DateUtil.format(startDate2, DatePattern.NORM_DATE_PATTERN);
        endDate2 = getNextWeekEndDay(startDate2);
        endDateStr2 = DateUtil.format(endDate2, DatePattern.NORM_DATE_PATTERN);
        startDate3 = getNextWeekStartDay(startDate2);
        startDateStr3 = DateUtil.format(startDate3, DatePattern.NORM_DATE_PATTERN);
        endDate3 = getNextWeekEndDay(startDate3);
        endDateStr3 = DateUtil.format(endDate3, DatePattern.NORM_DATE_PATTERN);
        startDate4 = getNextWeekStartDay(startDate3);
        startDateStr4 = DateUtil.format(startDate4, DatePattern.NORM_DATE_PATTERN);
        endDate4 = getNextWeekEndDay(startDate4);
        endDateStr4 = DateUtil.format(endDate4, DatePattern.NORM_DATE_PATTERN);
        startDate5 = getNextWeekStartDay(startDate4);
        startDateStr5 = DateUtil.format(startDate5, DatePattern.NORM_DATE_PATTERN);
        endDate5 = getNextWeekEndDay(startDate5);
        endDateStr5 = DateUtil.format(endDate5, DatePattern.NORM_DATE_PATTERN);
        startDate6 = getNextWeekStartDay(startDate5);
        startDateStr6 = DateUtil.format(startDate6, DatePattern.NORM_DATE_PATTERN);
        endDate6 = getNextWeekEndDay(startDate6);
        endDateStr6 = DateUtil.format(endDate6, DatePattern.NORM_DATE_PATTERN);
        startDate7 = getNextWeekStartDay(startDate6);
        startDateStr7 = DateUtil.format(startDate7, DatePattern.NORM_DATE_PATTERN);
        endDate7 = getNextWeekEndDay(startDate7);
        endDateStr7 = DateUtil.format(endDate7, DatePattern.NORM_DATE_PATTERN);
        startDate8 = getNextWeekStartDay(startDate7);
        startDateStr8 = DateUtil.format(startDate8, DatePattern.NORM_DATE_PATTERN);
        endDate8 = getNextWeekEndDay(startDate8);
        endDateStr8 = DateUtil.format(endDate8, DatePattern.NORM_DATE_PATTERN);
        startDate9 = getNextWeekStartDay(startDate8);
        startDateStr9 = DateUtil.format(startDate9, DatePattern.NORM_DATE_PATTERN);
        endDate9 = getNextWeekEndDay(startDate9);
        endDateStr9 = DateUtil.format(endDate9, DatePattern.NORM_DATE_PATTERN);
        startDate10 = getNextWeekStartDay(startDate9);
        startDateStr10 = DateUtil.format(startDate10, DatePattern.NORM_DATE_PATTERN);
        endDate10 = getNextWeekEndDay(startDate10);
        endDateStr10 = DateUtil.format(endDate10, DatePattern.NORM_DATE_PATTERN);
        startDate11 = getNextWeekStartDay(startDate10);
        startDateStr11 = DateUtil.format(startDate11, DatePattern.NORM_DATE_PATTERN);
        endDate11 = getNextWeekEndDay(startDate11);
        endDateStr11 = DateUtil.format(endDate11, DatePattern.NORM_DATE_PATTERN);
        startDate12 = getNextWeekStartDay(startDate11);
        startDateStr12 = DateUtil.format(startDate12, DatePattern.NORM_DATE_PATTERN);
        endDate12 = getNextWeekEndDay(startDate12);
        endDateStr12 = DateUtil.format(endDate12, DatePattern.NORM_DATE_PATTERN);
        startDate13 = getNextWeekStartDay(startDate12);
        startDateStr13 = DateUtil.format(startDate13, DatePattern.NORM_DATE_PATTERN);
        endDate13 = getNextWeekEndDay(startDate13);
        endDateStr13 = DateUtil.format(endDate13, DatePattern.NORM_DATE_PATTERN);
    }

    private static Date getNextWeekStartDay(Date date) {
        return DateUtil.offset(date, DateField.WEEK_OF_MONTH, 1);
    }

    private static Date getNextWeekEndDay(Date date) {
        return DateUtil.offset(date, DateField.DAY_OF_MONTH, 6);
    }

    private static Date getNextDay(Date date) {
        return DateUtil.offset(date, DateField.DAY_OF_MONTH, 1);
    }

    /**
     * 标记表是否处理（某门店）
     *
     * @param opSource
     * @return void
     **/
    public static void injectTableFlag(Set<Integer> opSource) {
        //是否含有直营门店表处理
        shopFlag = opSource.contains(OperationDataEnum.DIRECTLY_SHOP.getTableType());
        //是否含有直营预测表处理
        directlyFlag = opSource.contains(OperationDataEnum.PLAN_DIRECTLY.getTableType());
        executeFlag = opSource.contains(OperationDataEnum.DIRECTLY_EXECUTE.getTableType());
        accuracyFlag = opSource.contains(OperationDataEnum.DIRECTLY_ACCURACY.getTableType());
        differinFlag = opSource.contains(OperationDataEnum.DIRECTLY_DIFFERIN.getTableType());
        demandFlag = opSource.contains(OperationDataEnum.PLAN_DEMAND.getTableType());
    }

    public static void injectRemedyTimeCache(Date nowDate) {
        startTime = null;
        endTime = null;
        Date date = DateUtil.parse(CommonConstants.BIG_DATA_PULL_TIME);
        //如果当前时间小于该时间 且 当前时间在该时间2小时之前，则可以触发补救任务（取错误日志时间段：上一次job触发时间 至 下一次任务触发时间前两小时时间点）
        if (DateUtil.compare(nowDate, DateUtil.offset(date, DateField.HOUR_OF_DAY, -CommonConstants.BIG_DATA_OFFSET_VALUE)) < 0) {
            startTime = DateUtil.offset(date, DateField.DAY_OF_MONTH, -1);
            endTime = DateUtil.offset(date, DateField.HOUR_OF_DAY, -CommonConstants.BIG_DATA_OFFSET_VALUE);
            injectWeekCache(DateUtil.offset(date, DateField.DAY_OF_MONTH, -1));
            injectDayCache(DateUtil.offset(date, DateField.DAY_OF_MONTH, -1));
        }
        //如果当前时间大于该时间 且 当前时间在该时间2小时之后，则可以触发补救任务（取错误日志时间段：上一次job触发时间 至 下一次任务触发时间前两小时时间点）
        if (DateUtil.compare(nowDate, DateUtil.offset(date, DateField.HOUR_OF_DAY, CommonConstants.BIG_DATA_OFFSET_VALUE)) > 0) {
            startTime = date;
            endTime = DateUtil.offset(date, DateField.HOUR_OF_DAY, 24 - CommonConstants.BIG_DATA_OFFSET_VALUE);
            injectWeekCache(date);
            injectDayCache(date);
        }
    }
}
